/**
 * 短信发送记录列表
 */
var pageCurr;
$(function() {
    layui.use('table', function(){
        var table = layui.table
            ,form = layui.form;

        tableIns=table.render({
            elem: '#messageSendRecordList'
            ,url:'/messageSendRecord/getMessageSendRecords'
        	,method: 'post' //默认：get请求
            ,cellMinWidth: 80
            ,page: true,
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                ,limitName: 'limit' //每页数据量的参数名，默认：limit
            },response:{
                statusName: 'code' //数据状态的字段名称，默认：code
                ,statusCode: 200 //成功的状态码，默认：0
                ,countName: 'totals' //数据总数的字段名称，默认：count
                ,dataName: 'list' //数据列表的字段名称，默认：data
            }
            ,cols: [[
                {type:'numbers'}
                /*,{field:'id', title:'ID', width:80, unresize: true, sort: true,align:'center',hide:true}*/
                ,{field:'name', width:180, title:'发送模板名称',align:'center'}
                ,{field:'channel', width:180, title:'发送渠道',align:'center'}
                ,{field:'receiver', width:180, title: '接收人',align:'center'}
                ,{field:'isDel', title:'状态',width:120,align:'center',templet:'#isDelTpl'}
                ,{field:'insertTime', width:180, title: '添加时间',align:'center'}
                ,{fixed:'right', title:'操作', width:140,align:'center', toolbar:'#optBar'}
            ]]
            ,  done: function(res, curr, count){
            }
        });

        //监听工具条
        table.on('tool(messageSendRecordTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'view'){
                //查看
                getMessageSendRecord(data,data.id);
            }
        });

    });
    //搜索框
    layui.use(['form','laydate'], function(){
        var form = layui.form ,layer = layui.layer
            ,laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#qinsertTimeStart'
        });
        laydate.render({
            elem: '#qinsertTimeEnd'
        });
        //TODO 数据校验
        //监听搜索框
        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });
});

function cleanMessageSendRecord(){
	$("#name").val("");
    $("#channel").val("");
    $("#receiver").val("");
    $("#content").val("");
    $("#success").val("");
    $("#exceptionContext").val("");
    $("#insertTime").val("");
}

function openMessageSendRecord(title){
    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['550px'],
        content:$('#setMessageSendRecord'),
        end:function(){
        	cleanMessageSendRecord();
        }
    });
}
function getMessageSendRecord(obj,id) {
    //回显数据
    $.get("/messageSendRecord/getMessageSendRecord",{"id":id},function(data){
        if(isLogin(data)){
            if(data.msg=="ok" && data.messageSendRecord!=null){
                $("#name").val(data.messageSendRecord.name==null?'':data.messageSendRecord.name);
                $("#channel").val(data.messageSendRecord.channel==null?'':data.messageSendRecord.channel);
                $("#receiver").val(data.messageSendRecord.receiver==null?'':data.messageSendRecord.receiver);
                $("#success").val(data.messageSendRecord.success==null?'':data.messageSendRecord.success);
                $("#exceptionContext").val(data.messageSendRecord.exceptionContext==null?'':data.messageSendRecord.exceptionContext);
                $("#content").val(data.messageSendRecord.content==null?'':data.messageSendRecord.content);
                $("#insertTime").val(data.messageSendRecord.insertTime==null?'':data.messageSendRecord.insertTime);
                openMessageSendRecord("查看短信发送记录");
            }else{
                //弹出错误提示
                layer.alert(data.msg,function () {
                    layer.closeAll();
                });
            }
        }
    });
}

function load(obj){
    //重新加载table
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

